#include "ZService.h"

const int retry_seconds[] = {1, 2, 4, 8, 16, 32, 64};

void ZReconnectThread::Run() {
    while(!bStop) {
        int index;
        for(index = 0; index < infos->max_number; index++) {
            ZConnInfo& info = (*infos)[index];
            if(info.state == STATE_IDLE) {
                if(info.retry_current++ == retry_seconds[info.retry_count]) {
                    if(manager->Connect(&info.remote) >= 0) {
                        infos->Remove(index);
                    }
                    else {
                        info.retry_current = 0;
                        if(++info.retry_count == sizeof(retry_seconds)) info.retry_count = sizeof(retry_seconds) - 1;
                    }
                }
            }
        }
        ZThread::Sleep(1000);
    }
}
